<head>
<meta charset="UTF-8">
</head>
<div id="" class="easyui-panel" title="成绩管理" data-options = "fit:true" style="display: flex;flex-direction: column;">
	<p style="flex-grow: 0;">
		<label>选择班级</label>
		<input id="bj" class="easyui-combobox" data-options = "
			url:'banji/list',
			valueField:'id',
			textField:'name',
			panelHeight:200,
			onSelect:function(bj){
				bjIdse = bj.id;
			}
		" />
		<label>选择课程</label>
		<input id="stu" class="easyui-combobox" data-options = "
			url:'course/list',
			valueField:'id',
			textField:'name',
			panelHeight:200,
			onSelect:function(cou){
				var bjId = bjIdse;
				var couId = cou.id;
				var grid=$('#classgrade');
				var options=grid.datagrid('options');
				options.url='grade/classcourse?bjId='+bjId+'&couId='+couId;
				$('#classtable').show();
				grid.datagrid(options);
			}
		" />
	</p>
	<div id="classtable" style="flex-grow: 1;display: none;">
		<table id="classgrade" data-options = "
			onClickCell:function(index, field, value){
				 if (endEditing()) {  
					if(field=='grade'){
						 $(this).datagrid('beginEdit', index);
						 var ed=$(this).datagrid('getEditor',{field:field,index:index});
						 $(ed.target).focus();
					}
					editIndex = index;  
				}
			},
			onAfterEdit:function(index, row, changes){
				 var updated = $('#classgrade').datagrid('getChanges', 'updated');  
				 console.log(updated);
				     if (updated.length < 1) {  
				         editRow = undefined;  
				         $('#classgrade').datagrid('unselectAll');  
				         return;  
				     } else {  
				         // 传值  
				         submitForm(index, row, changes);  
				     }  
				 
			},
		">
			<thead>
				<tr>
					<th data-options="checkbox:true,field:'sas'">das</th>
					<th data-options="field:'id', hidden:true  ">ID</th>
					<th width="200" data-options="field:'student',
						formatter:function(value){
							if(value.name){
							return value.name;
							 }}">姓名</th>
					<th width="200" data-options="field:'grade'" editor="{type:'numberbox'}">成绩</th>
				</tr>
			</thead>
		</table>
	</div>
</div>
<script type="text/javascript">
	var editIndex = undefined;//定义编辑列的索引
	function endEditing() {//判断是否处于编辑状态
	    if (editIndex == undefined) { return true }
	    if ($('#classgrade').datagrid('validateRow', editIndex)) {
	        $('#classgrade').datagrid('endEdit', editIndex);
	        editIndex = undefined;
	        return true;
	    } else {
	        return false;
	    }
	}
	function submitForm(index, row, changes) {
	    var id=row.id;//成绩id  
	    var grade=changes.grade;//期末成绩  
	    var r =/^-?[1-9]/;//判断输入的是正整数  
	    if(!r.test(grade)){  
	        $.messager.alert('提醒', '请输入正整数！');  
	        return;  
	    }  
	    $.ajax({  
	            type : "post",  
	            async : false,  
	            url : "grade/save",  
	            data : {  
	                "id" : id,  
	                "grade":grade,
	            },  
	            success : function(data) {  
	                if(data==true){  
	                    $.messager.show({title:'提示',msg:'保存成功！'});  
	                    $("#classgrade").datagrid('reload');  
	                }  
	            }  
	        })  
	}
		var grid = $('#classgrade');
		grid.datagrid({
			fit:true,
			singleSelect:true,
			fitColumns:true,
			autoRowHeight:true,
			idField:'id',
			pageSize:10,
			sortName:'id',
			sortOrder:'asc',
			pagination:true,
			pageList:[5,10,25,30,50],
			toolbar:[$.crudgrid.buttons.refresh,'-'],
			dialog:{
				search:{
					title:'查询',
					href:'student/search',
				},
			},
		})

</script>

